package org.dreamfly.netshop.actions.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Namespace;
import org.dreamfly.netshop.entity.OrderInfo;
import org.dreamfly.netshop.entity.Type;
import org.dreamfly.netshop.manage.OrderInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.PropertyFilter.MatchType;
import org.springside.modules.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 用户管理Action. 使用Struts2 convention-plugin annotation定义Action参数.
 * 
 * @author calvin
 */
@Namespace(value = "/admin/order")
public class OrderAction extends ActionSupport {

    // @Autowired
    // private ClassManager classManager;
    //
    // @Autowired
    // private GoodsInfoManager goodsInfoManager;

    @Autowired
    private OrderInfoManager orderInfoManager;

    private List<Type> allClass;

    private String type;

    private Long id;

    private OrderInfo orderInfo;

    private Page<OrderInfo> orderInfoP = new Page<OrderInfo>(20);

    private List<OrderInfo> orderInfoL = new ArrayList<OrderInfo>(4);

    public List<Type> getAllClass() {
        return allClass;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public OrderInfo getOrderInfo() {
        return orderInfo;
    }

    public void setOrderInfo(OrderInfo orderInfo) {
        this.orderInfo = orderInfo;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Page<OrderInfo> getOrderInfoP() {
        return orderInfoP;
    }

    public void setOrderInfoP(Page<OrderInfo> orderInfoP) {
        this.orderInfoP = orderInfoP;
    }

    public List<OrderInfo> getOrderInfoL() {
        return orderInfoL;
    }

    public void setOrderInfoL(List<OrderInfo> orderInfoL) {
        this.orderInfoL = orderInfoL;
    }

    @Override
    public String execute() {
        // ActionContext ctx = ActionContext.getContext();
        // HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        // HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
        // classManager.delete((long) 21);
        return SUCCESS;
    }

    public String search() {
        HttpServletRequest request = Struts2Utils.getRequest();
        List<PropertyFilter> filters = new ArrayList<PropertyFilter>(); // HibernateWebUtils.buildPropertyFilters(request);
        // String id = request.getParameter("id");
        if ("orderId".equals(type) && id != null)
            filters.add(new PropertyFilter("id", id, MatchType.EQUAL));
        else if ("memberId".equals(type) && id != null) {
            filters.add(new PropertyFilter("member.id", id, MatchType.EQUAL));
        }
        String confirm = request.getParameter("orderInfo.isConfirm");
        String payment = request.getParameter("orderInfo.isPayment");
        String consignment = request.getParameter("orderInfo.isConsignment");
        if (!"".equals(confirm)) {
            filters.add(new PropertyFilter("isConfirm", confirm.equals("true"), MatchType.EQUAL));
        }
        if (!"".equals(payment)) {
            filters.add(new PropertyFilter("isPayment", payment.equals("true"), MatchType.EQUAL));
        }
        if (!"".equals(consignment)) {
            filters.add(new PropertyFilter("isConsignment", consignment.equals("true"), MatchType.EQUAL));
        }
        orderInfoP = orderInfoManager.search(orderInfoP, filters);
        return SUCCESS;
    }

    public String getByConfirm() {
        orderInfoP = orderInfoManager.getByConfirm("true".equals(type) ? true : false, orderInfoP);
        return SUCCESS;
    }

    public String getByPayment() {
        orderInfoP = orderInfoManager.getByPayment("true".equals(type) ? true : false, orderInfoP);
        return SUCCESS;
    }

    public String getByConsignment() {
        orderInfoP = orderInfoManager.getByConsignment("true".equals(type) ? true : false, orderInfoP);
        return SUCCESS;
    }

    public String deleteById() {
        orderInfoManager.delete(id);
        return SUCCESS;
    }

    public String getOrder() {
        orderInfo = orderInfoManager.get(id);
        return "info";
    }

    public String sendGoods() {
        orderInfo = orderInfoManager.get(id);
        orderInfo.setIsConsignment(true);
        orderInfoManager.save(orderInfo);
        return "info";
    }
}
